package com.frozendogtears.audiogame;

import java.util.List;

import com.echonest.api.v4.Artist;
import com.echonest.api.v4.ArtistParams;
import com.echonest.api.v4.EchoNestAPI;
import com.echonest.api.v4.EchoNestException;
import com.echonest.api.v4.Params;
import com.echonest.api.v4.Song;
import com.echonest.api.v4.SongParams;

public class SearchSongExample {

	private static final String API_KEY = "PFOJ2KAJFH0CDBYIE";
	private static final String TAG = "EchonestTest";
	private List<Song> songs;

	private EchoNestAPI echoNest;

	public SearchSongExample() throws EchoNestException {
		echoNest = new EchoNestAPI(API_KEY);
		echoNest.setTraceSends(false);
		echoNest.setTraceRecvs(false);		
	}

	public void dumpSong(Song song) throws EchoNestException {
		System.out.printf("%s\n", song.getTitle());
		System.out.printf("   artist: %s\n", song.getArtistName());
		System.out.printf("   dur   : %.3f\n", song.getDuration());
		System.out.printf("   BPM   : %.3f\n", song.getTempo());
		System.out.printf("   Mode  : %d\n", song.getMode());
		System.out.printf("   S hot : %.3f\n", song.getSongHotttnesss());
		System.out.printf("   A hot : %.3f\n", song.getArtistHotttnesss());
		System.out.printf("   A fam : %.3f\n", song.getArtistFamiliarity());
		System.out.printf("   A loc : %s\n", song.getArtistLocation());
	}

	public List<Song> searchSongByArtist(String artist, int results)
			throws EchoNestException {
		Params p = new Params();
		p.add("artist", artist);
		p.add("bucket", "audio_summary");
		p.add("results", results);
		p.add("sort", "song_hotttnesss-desc");

		return echoNest.searchSongs(p);
//		List<Song> songs = echoNest.searchSongs(p);
//		for (Song song : songs) {
//			System.out.printf("%.0f %s %s\n", song.getTempo(),
//					song.getArtistName(), song.getTitle());
//		}
	}

	public void searchForFastestSongsByArtist(String artist, int results)
			throws EchoNestException {
		ArtistParams ap = new ArtistParams();
		ap.addName(artist);
		List<Artist> artists = echoNest.searchArtists(ap);
		if (artists.size() > 0) {
			Params p = new Params();
			p.add("artist_id", artists.get(0).getID());
			p.add("bucket", "audio_summary");
			p.add("results", results);
			p.add("sort", "tempo-desc");

			List<Song> songs = echoNest.searchSongs(p);				
			
//			TrackAnalysis anal = songs.get(0).getAnalysis();
//			System.out.println(songs.get(0).getTimeSignature());
//			for (TimedEvent bar : anal.getBars())
//				System.out.println(bar.toString());	
			
//			try {
//				FileHandle file = Gdx.files.absolute("C:\\Users\\Daniel\\Eclipse workspace\\MyGame-Android\\assets\\music\\test.mp3");
//				if(file.file().exists())
//				{
//					return;
//				}
//				Track track;
//				
//				track = echoNest.uploadTrack(file.file(), true);				
//				AnalysisStatus status = track.waitForAnalysis(6000);
//				
//				if (status == AnalysisStatus.COMPLETE) {
//					System.out.println("Tempo in BPM: " + track.getTempo());
//				} 
//				else
//				{
//					System.out.println("Fail");
//				}
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}			
			
//			System.out.println(anal.getBeats().get(0).toString());
			for (Song song : songs) {
				System.out.printf("%.0f %s %s\n", song.getTempo(),
						song.getArtistName(), song.getTitle());
				System.out.println(song.getAnalysisURL().toString());
			}
		}
	}

	public List<Song> searchSongsByTitle(String title, int results)
			throws EchoNestException {
		Params p = new Params();
		p.add("title", title);
		p.add("results", results);
//		List<Song> songs = echoNest.searchSongs(p);
		
		return echoNest.searchSongs(p);
//		for (Song song : songs) {
//			dumpSong(song);
//			System.out.println();
//		}
	}

	public Double getTempo(String artistName, String title)
			throws EchoNestException {
		SongParams p = new SongParams();
		p.setArtist(artistName);
		p.setTitle(title);
		p.setResults(1);
		p.includeAudioSummary();
		List<Song> songs = echoNest.searchSongs(p);
		if (songs.size() > 0) {
			double tempo = songs.get(0).getTempo();
			return Double.valueOf(tempo);
		} else {
			return null;
		}
	}

	public void stats() {
		echoNest.showStats();
	}
}
